const Router = require('koa-router')
const router = new Router()
const readFilePromise = require('fs-readfile-promise')

//添加的新用户数据
var user = {
    "user4": {
        "name": "mohit",
        "password": "password4",
        "profession": "teacher",
        "id": 4
    }
}

router.get('/addUser', async (ctx, next) => {
    let results = await readFilePromise("./public/data/" + "users.json", 'utf8')

    console.log(results)
    //转成 JSON 数组
    let data = JSON.parse(results)

    data["user4"] = user["user4"]

    //返回结果中多出来一个 user4，但是并没有写回去文件
    ctx.body = {
        status: 'success',
        message: JSON.stringify(data)
    }

}
)

module.exports = router

